using UnityEngine;
using System.Collections;

public class ChoiceInventoryHandler : MonoBehaviour
{

	#region Choice Inventory Window Mode
	public enum ChoiceInventoryWindowModes{Items, Profiles}

	private ChoiceInventoryWindowModes _currentMode;
	public ChoiceInventoryWindowModes currentMode
	{
		get { return _currentMode; }

		set
		{
			_currentMode = value;
			if(_currentMode == ChoiceInventoryWindowModes.Items) 
			{
				for(int i = 0; i < EnableOnItems.Length; i++)
					EnableOnItems[i].SetActive(true);
				for(int i = 0; i < EnableOnProfiles.Length; i++)
					EnableOnProfiles[i].SetActive(false);
			}
			else
			{
				for(int i = 0; i < EnableOnItems.Length; i++)
					EnableOnItems[i].SetActive(false);
				for(int i = 0; i < EnableOnProfiles.Length; i++)
					EnableOnProfiles[i].SetActive(true);
			}
		}
	}
	#endregion

	#region Item Description Panel
	[System.Serializable]
	public class ItemDescriptionPanel
	{
		public GameObject WindowParent;
		public UILabel ItemName;
		public UILabel ItemDescription;
		public UITexture ItemImage;

		public ExogenesisItem AssignedItem = null;

		public void Assign(ExogenesisItem toAssign)
		{
			AssignedItem = toAssign;
			
			ItemName.text = AssignedItem.Name;
			ItemDescription.text = ItemHelper.SanitizeString(AssignedItem.Description);
			
			ItemImage.mainTexture = AssignedItem.LoadItemTexture();
		}
		
		public void ClearData()
		{
			ItemName.text = "";
			ItemDescription.text = "";
			ItemImage.mainTexture = Resources.Load("BG/black", typeof(Texture)) as Texture;
		}
	}

	public ItemDescriptionPanel itemDescriptionPanel;
	#endregion

	#region Profile Description Panel
	[System.Serializable]
	public class ProfileDescriptionPanel{
		public GameObject WindowParent;
		
		public UILabel ProfileName;
		public UILabel ProfileDescription;
		public UITexture ProfileImage;
		
		public ProfileData AssignedProfile = null;
		
		public void Assign(ProfileData profileData)
		{
			AssignedProfile = profileData;
			
			ProfileName.text = AssignedProfile.name;
			ProfileDescription.text = ProfileReader.SanitizeString(AssignedProfile.description);
			
			ProfileImage.mainTexture = AssignedProfile.LoadProfileTexture();
		}
		
		public void ClearData()
		{
			ProfileName.text = "";
			ProfileDescription.text = "";
			ProfileImage.mainTexture = Resources.Load("BG/black", typeof(Texture)) as Texture;
		}
	}
	
	public ProfileDescriptionPanel profileDescriptionPanel;
	#endregion

	public GameObject[] EnableOnItems;
	public GameObject[] EnableOnProfiles;

	public GameObject ItemsPanel;
	public GameObject ItemPrefab;
	public GameObject ProfilePrefab;

	public GameObject btnChoose;

	public SampleInventoryHandler sih;

	private bool repositionFlag = false;
	private bool callOnce;

	void Awake()
	{
		sih = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>();
		btnChoose = GameObject.Find("Choose");
		btnChoose.GetComponent<UIButtonMessage>().target = gameObject;
		btnChoose.GetComponent<UIButtonMessage>().functionName = "ChooseClicked";
		callOnce = false;
	}

	// Use this for initialization
	void Start ()
	{
		currentMode = ChoiceInventoryWindowModes.Items;
		SpawnSampleInventory();

	}
			// Update is called once per frame
	void Update ()
	{
		if(callOnce == false)
		{
			Activate();
			callOnce = true;
		}
		if(repositionFlag)
		{
			repositionFlag = false;
			ItemsPanel.GetComponent<UIGrid>().repositionNow = true;
		}
	}

	#region Item Button Functions

	void ClearItemPanel()
	{
		Collider[] trans = ItemsPanel.GetComponentsInChildren<Collider>();
		for(int i = 0; i < trans.Length; i++)
			Destroy (trans[i].gameObject);

		itemDescriptionPanel.ClearData();

		ItemsPanel.transform.localPosition = new Vector3(11.98f,835.0f,0);
		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void SpawnSampleInventory()
	{
		foreach(ExogenesisItem item in sih.playerInventory)
		{
			if(itemDescriptionPanel.AssignedItem.ID == -1)
				itemDescriptionPanel.Assign(item);

			GameObject go = Instantiate(ItemPrefab) as GameObject;
			go.transform.parent = ItemsPanel.transform;
			go.transform.localScale = Vector3.one;

			go.GetComponent<ItemItemHandler>().Assign(item);
			go.GetComponent<UIButtonMessage>().target = gameObject;
			go.GetComponent<UIButtonMessage>().functionName = "ItemClicked";
		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void ItemClicked(GameObject item)
	{
		ExogenesisItem assignedItem = item.GetComponent<ItemItemHandler>().assignedItem;

		itemDescriptionPanel.Assign(assignedItem);

		if(btnChoose.activeSelf == false)
			btnChoose.SetActive(true);
	}
	#endregion

	#region Profile Button Functions

	void ClearProfilePanel()
	{
		Collider[] trans = ItemsPanel.GetComponentsInChildren<Collider>();

		for(int i = 0; i < trans.Length; i++)
			Destroy(trans[i].gameObject);

		profileDescriptionPanel.ClearData();

		ItemsPanel.transform.localPosition = new Vector3(11.98f, 420.0f, 0);
		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void spawnProfiles()
	{
		foreach(ProfileData item in sih.profiles)
		{
			if(profileDescriptionPanel.AssignedProfile.ID == -1)
				profileDescriptionPanel.Assign(item);

			GameObject go = Instantiate(ProfilePrefab) as GameObject;
			go.transform.parent = ItemsPanel.transform;
			go.transform.localScale = Vector3.one;

			go.GetComponent<ProfileHandler>().Assign(item);
			go.GetComponent<UIButtonMessage>().target = gameObject;
			go.GetComponent<UIButtonMessage>().functionName = "ProfileClicked";
		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void ProfileClicked(GameObject item)
	{
		ProfileData selectedItem = item.GetComponent<ProfileHandler>().assignedProfile;
		
		profileDescriptionPanel.Assign(selectedItem);

		if(btnChoose.activeSelf == false)
			btnChoose.SetActive(true);
	}


	#endregion

	void MainInventoryButtonClick(GameObject sender)
	{
		if(sender.name.Contains("Item"))
		{
			currentMode = ChoiceInventoryWindowModes.Items;
			ClearProfilePanel();
			SpawnSampleInventory();
			btnChoose.SetActive(false);
		}
		else
		{
			currentMode = ChoiceInventoryWindowModes.Profiles;
			ClearItemPanel();
			spawnProfiles();
			btnChoose.SetActive(false);
		}
	}

	public void Activate()
	{
		currentMode = ChoiceInventoryWindowModes.Items;
		SpawnSampleInventory();
	}

	void ChooseClicked(GameObject item)
	{
		int checkDialogue = PlayerPrefs.GetInt("LastDia");
		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		switch(checkDialogue)
		{
			case 68:
			checkItemTanto();
			break;
			case 76:
			for(int i = 0; i < sih.profiles.Count; i++)
			{
				if(sih.profiles[i].name == "Haru")
				{
					checkProfileHaru();
					return;
				}
			}
			scm.PlayDialogue("77");
			break;
			case 84:
			checkProfileHaru();
			break;
			case 185:
			checkItemPoster();
			break;
			case 274:
			checkScrewdriver();
			break;
		}
	}

	void checkItemTanto()
	{
		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		switch(itemDescriptionPanel.ItemName.text)
		{
			case "Sanctus Bio-Chip":
			scm.PlayDialogue("69");
			break;
			case "Miho's Memento":
			scm.PlayDialogue("70");
			break;
			case "Tanto Blade":
			scm.PlayDialogue("71");
			break;
		}
	}

	void checkProfileHaru()
	{
		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		switch(profileDescriptionPanel.ProfileName.text)
		{
			case "Haru":
			if(profileDescriptionPanel.ProfileDescription.text.Contains("He's heard a lot about Durchhalten from Eiji."))
				scm.PlayDialogue("85");
			break;
			default:
			scm.PlayDialogue("84");
			break;
		}
	}

	void checkItemPoster()
	{
		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		switch(itemDescriptionPanel.ItemName.text)
		{
		case "Sanctus Bio-Chip":
			scm.PlayDialogue("197");
			break;
		case "Miho's Memento":
			scm.PlayDialogue("198");
			break;
		case "Tanto Blade":
			scm.PlayDialogue("199");
			break;
		case "Nomad Poster":
			scm.PlayDialogue("341");
			break;
		}
	}

	void checkScrewdriver()
	{
		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		switch(itemDescriptionPanel.ItemName.text)
		{
			case "Screw Driver":
			scm.PlayDialogue("277");
			break;
			default:
			scm.PlayDialogue("276");
			break;
		}
	}
}

